//
//  MessageFontSettingViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/6/15.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "MessageFontSettingViewController.h"
#import "Masonry.h"
@interface MessageFontSettingViewController ()
@property(nonatomic,strong)UILabel *showLabel;
@property(nonatomic,assign)CGFloat fontSize;
@property(nonatomic,strong)UISlider *slider;
@end

@implementation MessageFontSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setNewTitleCenter:@"字体大小"];
    [self.view setBackgroundColor:RGBCOLOR(243, 243, 243)];
    [self setUpShowFontLable];
    [self setupSliderView];
}
-(void)setUpShowFontLable
{
    self.showLabel = [[UILabel alloc]init];
    [self.view addSubview:_showLabel];
    [_showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(100);
        make.top.mas_equalTo(0);
    }];
    _showLabel.textAlignment = NSTextAlignmentCenter;
  _showLabel.text = @"拖动滑块改变文字大小";
}
-(void)setupSliderView
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.showLabel).mas_equalTo(100);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
    self.slider = [[UISlider alloc]init];
    self.slider.maximumValue = 22;
    self.slider.minimumValue = 10;
    self.slider.continuous = YES;
    self.slider.value = [USER_DEFAULT floatForKey:@"messageFontSize"];
    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    _slider.minimumTrackTintColor = RGBCOLOR(182, 182, 182);
    [view addSubview:self.slider];
    [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(40);
        make.right.mas_equalTo(-10);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action: @selector(tap:)];
    [self.slider addGestureRecognizer:tap];
    UILabel *leftFont = [[UILabel alloc]init];
    [view addSubview:leftFont];
    leftFont.text = @"A";
    leftFont.textAlignment =NSTextAlignmentLeft;
    leftFont.font = [UIFont systemFontOfSize:10];
    [leftFont mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.height.width.mas_equalTo(40);
    }];
    UILabel *rightFont = [[UILabel alloc]init];
    [view addSubview:rightFont];
    rightFont.text = @"A";
    rightFont.textAlignment = NSTextAlignmentRight;
    rightFont.font = [UIFont systemFontOfSize:22];
    [rightFont mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.width.mas_equalTo(40);
    }];
    
}
-(void)sliderValueChanged:(id)sender
{
    UISlider* control = (UISlider*)sender;
    if(control == self.slider){
        float value = control.value;
        [USER_DEFAULT setFloat:value forKey:@"messageFontSize"];
        [USER_DEFAULT synchronize];
         self.showLabel.font =[UIFont systemFontOfSize:[USER_DEFAULT floatForKey:@"messageFontSize"]];
        [self.showLabel setNeedsDisplay];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (NSInteger i = 0; i<7; i++) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor= RGBCOLOR(182, 182, 182);
        CGFloat width = (self.view.width - 20)/6.0;
        if (i == 0 || i==6) {
            view.frame = CGRectMake(i * width + 2,10, 1, 10);
        }
        else
        {
        view.frame = CGRectMake(i * width,10, 1, 10);
        }
        [self.slider addSubview:view];
    }
}
-(void)tap:(UIGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateEnded) {
        CGPoint location = [sender locationInView:self.view];
        float x = location.x;
        float r = x/self.view.width;
        float value = (self.slider.maximumValue-self.slider.minimumValue) * r + 10;
        [self.slider setValue:value animated:YES];
        [USER_DEFAULT setFloat:value forKey:@"messageFontSize"];
        [USER_DEFAULT synchronize];
        self.showLabel.font =[UIFont systemFontOfSize:[USER_DEFAULT floatForKey:@"messageFontSize"]];
        [self.showLabel setNeedsDisplay];
    }
}
@end
